package com.vol.upload;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
/**
 * 
 * @author 王宗好 
 *
 */
public class Upload extends HttpServlet {
	/**  
	 *   
	 */
	private static final long serialVersionUID = 7440302204266787092L;


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String savePath = "d:/";//保存路径

		String uploadPath = "d:/"; //上传路径

		try {
			System.out.println("开始进行文件上传");
			DiskFileUpload fu = new DiskFileUpload();
			fu.setSizeMax(100*1024*1024); // ��������ļ��ߴ最大������100MB   
			fu.setSizeThreshold(4096); // ���û������С��缓存4kb   
			fu.setRepositoryPath(uploadPath); // ������ʱĿ¼   
			List fileItems = fu.parseRequest(request);
			Iterator i = fileItems.iterator();
			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				String fileName = fi.getName();// ����ļ�������ļ����
				if (fileName != null) {
					fi.write(new File(savePath + fileName));
				}
			}
			response.setContentType("text/html;charset=UTF-8");
			System.out.println("上传成功");
			response.getWriter().print(
					"{'success':true,'message':'上传成功'}");
		} catch (Exception e) {
			System.out.println("上传失败");
			response.getWriter().print(
					"{'success':false,'message':'上传失败'}");
		}

	}
}
